package Set集合排序;

import java.util.Comparator;
import java.util.TreeSet;

public class 成绩排序案例 {
    public static void main(String[] args) {
        /*创建TreeSet集合对象，通过比较器排序进行排序*/
        TreeSet<Student2> ts = new TreeSet<Student2>(new Comparator<Student2>() {
            @Override
            public int compare(Student2 s1, Student2 s2) {
                /*主要条件*/
                int num=s2.getSum()-s1.getSum();
                /*次要条件*/
                int num2=num==0?s1.getChinese()-s2.getChinese():num;
                int num3=num2==0?s1.getName().compareTo(s2.getName()):num2;
                return num3;
            }
        });
        /*创建学生对象*/
        Student2 s1 = new Student2("林青霞", 98, 100);
        Student2 s2 = new Student2("张曼玉", 95, 95);
        Student2 s3 = new Student2("王祖贤", 100, 93);
        Student2 s4 = new Student2("刘燕", 98, 97);
        Student2 s5 = new Student2("风清扬", 97, 98);
        Student2 s6 = new Student2("左冷禅", 97, 99);
        Student2 s7 = new Student2("赵云", 96, 99);
        /*把学生对象添加到集合*/
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);
        ts.add(s6);
        ts.add(s7);
        /*遍历集合*/
        for (Student2 s:ts) {
            System.out.println(s.getName()+","+s.getChinese()+","+s.getMath()+","+s.getSum());
        }
    }
}
/*
* 学生类
* */
class Student2{
    private String name;
    private int chinese;
    private int math;

    public Student2() {
    }

    public Student2(String name, int chinese, int math) {
        this.name = name;
        this.chinese = chinese;
        this.math = math;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChinese() {
        return chinese;
    }

    public void setChinese(int chinese) {
        this.chinese = chinese;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }
    public int getSum(){
        return this.chinese+this.math;
    }
}